package com.weipeep.sys.controller;


import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.weipeep.sys.domain.WebUserDO;
import com.weipeep.sys.service.WebUserService;
import com.weipeep.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-04-20 22:33:58 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/webUser")
public class WebUserController {
	@Autowired
	private WebUserService webUserService;
	
	@GetMapping()
	String WebUser(){
	    return "sys/webUser/webUser";
	}
	@RequiresPermissions("admin")
	@ResponseBody
	@GetMapping("/list")
	public Result<Page<WebUserDO>> list(Integer pageNumber, Integer pageSize, WebUserDO webUserDTO){
		// 查询列表数据
        Page<WebUserDO> page = new Page<>(pageNumber, pageSize);
        String company = webUserDTO.getCompany();
		webUserDTO.setCompany(null);
        Wrapper<WebUserDO> wrapper = new EntityWrapper<WebUserDO>(webUserDTO);
		wrapper.orderBy("jiaofee_end_time asc");
		if (company !=null && company.trim().length()>0){
			wrapper.like("company","%"+company+"%");
		}
        page = webUserService.selectPage(page, wrapper);
        int total = webUserService.selectCount(wrapper);
        page.setTotal(total);
        return Result.ok(page);
	}
	@RequiresPermissions("admin")
	@GetMapping("/add")
	String add(){
	    return "sys/webUser/add";
	}

	@RequiresPermissions("admin")
	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		WebUserDO webUser = webUserService.selectById(id);
		model.addAttribute("webUser", webUser);
	    return "sys/webUser/edit";
	}
	
	/**
	 * 保存
	 */
	@RequiresPermissions("admin")
	@ResponseBody
	@PostMapping("/save")
	public Result<String> save( WebUserDO webUser){
		if(webUserService.insert(webUser)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@RequiresPermissions("admin")
	@ResponseBody
	@RequestMapping("/update")
	public Result<String>  update( WebUserDO webUser){
		webUserService.updateById(webUser);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("admin")
	@PostMapping( "/remove")
	@ResponseBody
	public Result<String>  remove( Long id){
		if(webUserService.deleteWebUserDOById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("admin")
	@PostMapping( "/batchRemove")
	@ResponseBody
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		webUserService.deleteBatchWebUserDOIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
